package com.njtoyo.taxi.admin.config.doc;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Dell
 * @Date 2021/6/1 14:52
 */
@Configuration
@EnableSwagger2WebMvc
@ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true")
public class SwaggerConfig {

    @Bean
    public Docket generalApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/general/**")).build().groupName("general").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket platformApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/platform/**")).build().groupName("平台管理").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket businessApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/business/**")).build().groupName("业务管理").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket openApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/open/**")).build().groupName("开放资源").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket dataApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/data/**")).build().groupName("数据管理").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket marketingApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/marketing/**")).build().groupName("营销管理").pathMapping("/")
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket allApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.njtoyo.taxi.admin.rest.controller"))
                .build().groupName("all").globalOperationParameters(setHeaderToken())
                .apiInfo(apiInfo());
    }

    private List<Parameter> setHeaderToken() {
        List<Parameter> pars = new ArrayList<>();
        List<ParameterBuilder> builders = Arrays.asList(new ParameterBuilder().name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false));
        builders.forEach(e -> pars.add(e.build()));
        return pars;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("叫车管理后台接口文档")
                .description("叫车管理后台接口文档")
                .version("1.0")
                .license("njtoyo")
                .build();
    }

}
